package jaux.tank.api.controller.system;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;
import jaux.tank.api.controller.BaseController;
import jaux.tank.bean.annotion.core.BussinessLog;
import jaux.tank.bean.constant.factory.PageFactory;
import jaux.tank.bean.dto.CfgDto;
import jaux.tank.bean.entity.system.Cfg;
import jaux.tank.bean.enumeration.BizExceptionEnum;
import jaux.tank.bean.exception.TankException;
import jaux.tank.bean.vo.front.Ret;
import jaux.tank.bean.vo.front.Rets;
import jaux.tank.service.system.CfgService;
import jaux.tank.utils.Maps;
import jaux.tank.utils.ToolUtil;
import jaux.tank.utils.factory.Page;

/**
 * CfgController
 *
 * @author zt
 * @version 2018/11/17 0017
 */
@RestController("cfgApiController")
@RequestMapping("/api/cfg")
public class CfgController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private CfgService cfgService;

    /**
     * 查询操作日志列表
     */
    @ApiOperation(value = "查询配置参数")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Ret<Page<Cfg>> list(@RequestParam(required = false) String cfgName, @RequestParam(required = false) String cfgValue) {
        Page<Cfg> page = new PageFactory<Cfg>().defaultPage();

        page = cfgService.findPage(page, Maps.newHashMap("cfgName",cfgName,"cfgValue",cfgValue));
        page.setRecords(page.getRecords());
        return Rets.success(page);
    }
    @ApiOperation(value = "编辑参数")
    @PostMapping
    @BussinessLog(value = "编辑参数", key = "cfgName")
    public Ret<Object> save(@ModelAttribute CfgDto cfg){
        if (ToolUtil.isOneEmpty(cfg, cfg.getCfgName(),cfg.getCfgValue())) {
            throw new TankException(BizExceptionEnum.REQUEST_NULL);
        }
        cfgService.save(cfg.toEntity());
        return Rets.success();
    }
    @ApiOperation(value = "删除参数")
    @RequestMapping(method = RequestMethod.DELETE)
    @BussinessLog(value = "删除参数", key = "id")
    public Ret<Object> remove(String id){
        logger.info("id:{}",id);
        if (ToolUtil.isEmpty(id)) {
            throw new TankException(BizExceptionEnum.REQUEST_NULL);
        }
        cfgService.delete(id);
        return Rets.success();
    }
}
